/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the koala4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * @Author: GuoYang
 */

package com.koalacloud.koala.zhaikucloud.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.koalacloud.koala.common.core.util.R;
import com.koalacloud.koala.common.log.annotation.SysLog;
import com.koalacloud.koala.common.security.annotation.Inner;
import com.koalacloud.koala.zhaikucloud.entity.MallPanel;
import com.koalacloud.koala.zhaikucloud.service.MallPanelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 版块分类
 *
 * @Author: GuoYang
 * @date 2022-02-07 15:33:09
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/mallPanel")
@Tag(description = "mallPanel" , name = "版块分类管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class MallPanelController {

    private final MallPanelService mallPanelService;

    /**
     * 分页查询
     *
     * @return
     */
    @Inner(value = false)
    @Operation(summary = "全部版块", description = "全部版块")
    @GetMapping("/all")
    @Cacheable(value = "mall_panel", key = "#root.methodName", unless = "#result == null ")
    public R getMallPanelAll() {
        return R.ok(mallPanelService.getPanelAll());
    }

    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param mallPanel 版块分类
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('mallPanel_view')")
    public R getMallPanelPage(Page page, MallPanel mallPanel) {
        return R.ok(mallPanelService.page(page, Wrappers.query(mallPanel)));
    }


    /**
     * 通过id查询版块分类
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('mallPanel_view')")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(mallPanelService.getById(id));
    }

    /**
     * 新增版块分类
     *
     * @param mallPanel 版块分类
     * @return R
     */
    @Operation(summary = "新增版块分类", description = "新增版块分类")
    @SysLog("新增版块分类")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('mallPanel_add')")
    @CacheEvict(value = "mall_panel", allEntries = true)
    public R save(@RequestBody MallPanel mallPanel) {
        return R.ok(mallPanelService.save(mallPanel));
    }

    /**
     * 修改版块分类
     *
     * @param mallPanel 版块分类
     * @return R
     */
    @Operation(summary = "修改版块分类", description = "修改版块分类")
    @SysLog("修改版块分类")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('mallPanel_edit')")
    @CacheEvict(value = "mall_panel", allEntries = true)
    public R updateById(@RequestBody MallPanel mallPanel) {
        return R.ok(mallPanelService.updateById(mallPanel));
    }

    /**
     * 通过id删除版块分类
     *
     * @param ids id 列表
     * @return R
     */
    @Operation(summary = "通过id删除版块分类", description = "通过id删除版块分类")
    @SysLog("通过id删除版块分类")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('mallPanel_del')")
    @CacheEvict(value = "mall_panel", allEntries = true)
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(mallPanelService.removeBatchByIds(CollUtil.toList(ids)));
    }

    @GetMapping(value = "/tree")
    public R<List<Tree<Integer>>> getTree(boolean lazy, Integer parentId) {
        return R.ok(mallPanelService.getTree(lazy, parentId));
    }

}
